package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

public class RegDemo {
    public static <UserInfo> void main(String[] args) throws Exception{
        Connection conn=DBUtil.getConnection();

//            Class.forName("com.mysql.cj.jdbc.Driver");
//            Connection conn = DriverManager.getConnection(
//                    "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
//                    "root",
//                    "root"
//            );

//        System.out.println("与数据库链接成功！");
//
//        System.out.println("请输入用户名：");
//        String username=scanner.nextLine();
//
//        System.out.println("请输入用户密码：");
//        String password=scanner.nextLine();
//
//        System.out.println("请输入用户昵称：");
//        String nickname=scanner.nextLine();
//
//        System.out.println("请输入用户年龄：");
//        int age=scanner.nextInt();



        Userinfo userInfo = InputUtil.getInputObject(new Userinfo(),"欢迎注册");
        System.out.println(userInfo);


            String sql = "INSERT INTO userinfo (username,password,nickname,age)" +
                    "VALUES('" + userInfo.getUsername() + "', '" + userInfo.getPassword() + "', '" + userInfo.getNickname() + "', " + userInfo.getAge() + ")";
            Statement stmt = conn.createStatement();
            int num = stmt.executeUpdate(sql);
            System.out.println("执行成功,添加"+num+"数据");
            System.out.println("用户的帐号是：" + userInfo.getUsername() + "用户的密码是" + userInfo.getPassword() + "用户的昵称是" + userInfo.getNickname() + "用户的年龄是" + userInfo.getAge());
            System.out.println(num>0?"注册成功":"注册失败");;
            //上面语句执行完，到这里要关闭连接，conn.close();就是针对关闭连接的。
            conn.close();








    }


}
